#include "mousecondmodel.h"


MouseCondModel::MouseCondModel(AED_MouseComp *comp, QObject *parent) :
    QAbstractTableModel(parent),m_comp(comp)
{
}

int MouseCondModel::rowCount(const QModelIndex & parent) const{
    return m_comp->GetConditionList()->size();
}

int MouseCondModel::columnCount(const QModelIndex & parent) const{
    return 4;
}

QVariant MouseCondModel::data ( const QModelIndex & index, int role) const{
    if(role==Qt::DisplayRole||role==Qt::EditRole){
        int row = index.row();

        AED_MouseComp::Condition cond = m_comp->GetConditionList()->operator [](row);


        QVariant var;

        if(index.column()==0)
            var = cond.name;
        else if(index.column()==1)
            var = cond.type;
        else if(index.column()==2)
            var = cond.omega;
        else if(index.column()==3)
            var = cond.inv;
        else
            return QVariant(QVariant::Invalid);
        return var;
    }
    return QVariant(QVariant::Invalid);
}

QVariant MouseCondModel::headerData ( int section, Qt::Orientation orientation, int role) const{
    if(orientation==Qt::Horizontal){
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            if(section==0)
                return tr("Name");
            if(section==1)
                return tr("Type");
            if(section==2)
                return tr("Omega");
            if(section==3)
                return tr("Inverted");
        }else{
            return QVariant(QVariant::Invalid);
        }
    }else{
        if (role==Qt::DisplayRole||role==Qt::EditRole){
            QVariant h(QVariant::Int);
            h=section+1;
            return h;
        }else{
            return QVariant(QVariant::Invalid);
        }
    }
    return QVariant(QVariant::Invalid);
}

void MouseCondModel::AddCondition(){
    m_comp->AddCondition("NEW_COND","left_button", 1, false);

    emit layoutChanged();
}

bool MouseCondModel::setData(const QModelIndex &index, const QVariant &value, int role){
    int row = index.row();
    int column = index.column();

    if(index.isValid() && role==Qt::EditRole){
        if(column==0){
            m_comp->GetConditionList()->operator [](row).name = value.toString();
        }else if(column==1){
            m_comp->GetConditionList()->operator [](row).type = value.toString();
        }else if(column==2){
            m_comp->GetConditionList()->operator [](row).omega = value.toInt();
        }else if(column==3){
            m_comp->GetConditionList()->operator [](row).inv = value.toBool();
        }else{
            return false;
        }
        emit dataChanged(index,index);
        return true;
    }

    return false;
}

Qt::ItemFlags MouseCondModel::flags(const QModelIndex &index) const{
    Qt::ItemFlags flags = Qt::ItemIsEnabled|Qt::ItemIsEditable|Qt::ItemIsSelectable;
    return flags;
}

void MouseCondModel::RemoveCondition(int index){
    m_comp->RemoveCondition(index);
    emit layoutChanged();
}
